import axios from "axios"
import {Message} from 'element-ui'
import router from "../router/index"

axios.defaults.baseURL = "http://127.0.0.1:3000/api"

axios.interceptors.request.use(
    function(request){
        const storage = JSON.parse(localStorage.getItem("loginData"||'{}'))
        if(storage){
            request.headers.Authorization = storage.token
        }
        return request
    }
)

axios.interceptors.response.use(
    function(response){
        //响应如果遇到token过期，要求从新登录 403
        if(response.data.code === 403){
            Message.error(response.data.msg) //外部单独调用message组件的格式
            //内部调用this.$message({type:"error",})
            console.log(router)
            router.push("/login")
        }
        return response.data // 根据axios接口惯例，过滤多余的内容
    }
)
export default axios